package com.jincheng.web.controller.system;

import com.jincheng.common.annotation.Log;
import com.jincheng.common.core.controller.BaseController;
import com.jincheng.common.core.domain.AjaxResult;
import com.jincheng.common.core.page.TableDataInfo;
import com.jincheng.common.enums.BusinessType;
import com.jincheng.common.utils.poi.ExcelUtil;
import com.jincheng.system.domain.Comment;
import com.jincheng.system.domain.dto.CommentDTO;
import com.jincheng.system.service.ICommentService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评论Controller
 *
 * @author DairyJay
 * @date 2025-03-02
 */
@RestController
@RequestMapping("/system/comment")
public class CommentController extends BaseController {
    private final ICommentService commentService;

    public CommentController(ICommentService commentService) {
        this.commentService = commentService;
    }

    /**
     * 查询评论列表
     */
    @PreAuthorize("@ss.hasPermi('system:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Comment comment) {
        startPage();
        List<Comment> list = commentService.selectCommentList(comment);
        return getDataTable(list);
    }

    /**
     * 根据活动id查询评论列表，包含当前用户点赞信息
     */
    @GetMapping("/listByActivityId/{activityId}")
    public TableDataInfo listByActivityId(@PathVariable Long activityId) {
        startPage();
        List<CommentDTO> list = commentService.selectCommentListByActivityId(activityId);
        return getDataTable(list);
    }


    /**
     * 导出评论列表
     */
    @PreAuthorize("@ss.hasPermi('system:comment:export')")
    @Log(title = "评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Comment comment) {
        List<Comment> list = commentService.selectCommentList(comment);
        ExcelUtil<Comment> util = new ExcelUtil<>(Comment.class);
        util.exportExcel(response, list, "评论数据");
    }

    /**
     * 获取评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:comment:query')")
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Long commentId) {
        return success(commentService.selectCommentByCommentId(commentId));
    }

    /**
     * 新增评论
     */
    @PreAuthorize("@ss.hasPermi('system:comment:add')")
    @Log(title = "评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Comment comment) {
        return toAjax(commentService.insertComment(comment));
    }


    /**
     * 发表评论
     */
    @Log(title = "评论", businessType = BusinessType.INSERT)
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody Comment comment) {
        return toAjax(commentService.publishComment(comment));
    }


    /**
     * 修改评论
     */
    @PreAuthorize("@ss.hasPermi('system:comment:edit')")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Comment comment) {
        return toAjax(commentService.updateComment(comment));
    }

    /**
     * 修改用户评论点赞信息
     */
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PutMapping("/like")
    public AjaxResult editByLike(@RequestBody CommentDTO commentDTO) {
        commentDTO.setUserId(getUserId());
        return toAjax(commentService.updateCommentAndLike(commentDTO));
    }

    /**
     * 删除评论
     */
    @PreAuthorize("@ss.hasPermi('system:comment:remove')")
    @Log(title = "评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable Long[] commentIds) {
        return toAjax(commentService.deleteCommentByCommentIds(commentIds));
    }
}
